﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Data;

namespace DNNSocialBookmark.Core
{
    /// <summary>
    /// 
    /// </summary>
    public class Bookmark 
    {
        /// <summary>
        /// 
        /// </summary>
        public short ID { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string BaseUri {get;set;}
        /// <summary>
        /// 
        /// </summary>
        public string SourceUrl { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Username { get;set; }
        /// <summary>
        /// 
        /// </summary>
        public string Password { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string Description { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public virtual void Post()
        { 
        
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual void Add()
        {
            string connectionString = "EduGetItFromConfigurationFile";
            SqlConnection connection = new SqlConnection(connectionString);
            SqlCommand command = connection.CreateCommand();
            command.CommandText = "AddStoredProcedureName";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@Name", Name));
            command.Parameters.Add(new SqlParameter("@Description", Description));
            command.Parameters.Add(new SqlParameter("@BaseUri", BaseUri));
            command.Parameters.Add(new SqlParameter("@SourceUrl", SourceUrl));
            command.Parameters.Add(new SqlParameter("@Username", Username));
            command.Parameters.Add(new SqlParameter("@Password", Password));
            command.Parameters.Add(new SqlParameter("@Description", Description));
            try
            {
                connection.Open();
                int count = command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {

            }
            finally
            {
                if (connection != null)
                {
                    connection.Dispose();
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual void Edit() 
        {
            string connectionString = "EduGetItFromConfigurationFile";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "EditStoredProcedureName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@ID", ID));
                command.Parameters.Add(new SqlParameter("@Name", Name));
                command.Parameters.Add(new SqlParameter("@Description", Description));
                command.Parameters.Add(new SqlParameter("@BaseUri", BaseUri));
                command.Parameters.Add(new SqlParameter("@SourceUrl", SourceUrl));
                command.Parameters.Add(new SqlParameter("@Username", Username));
                command.Parameters.Add(new SqlParameter("@Password", Password));
                command.Parameters.Add(new SqlParameter("@Description", Description));
                try
                {
                    connection.Open();
                    int count = command.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {

                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual void Find()
        {
            string connectionString = "EduGetItFromConfigurationFile";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = connection.CreateCommand();
                SqlDataReader reader = null;                    
                command.CommandText = "FindStoredProcedureName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@ID", ID));
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        if (reader["Name"] != DBNull.Value)
                        {
                            this.Name = Convert.ToString(reader["Name"]);
                        }
                        if (reader["Description"] != DBNull.Value)
                        {
                            this.Name = Convert.ToString(reader["Description"]);
                        }
                        if (reader["BaseUri"] != DBNull.Value)
                        {
                            this.Name = Convert.ToString(reader["BaseUri"]);
                        }
                        if (reader["Username"] != DBNull.Value)
                        {
                            this.Name = Convert.ToString(reader["Username"]);
                        }
                        if (reader["Password"] != DBNull.Value)
                        {
                            this.Name = Convert.ToString(reader["Password"]);
                        }
                    }
                }
                catch (SqlException ex)
                {

                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public virtual void Delete()
        {
            string connectionString = "EduGetItFromConfigurationFile";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = connection.CreateCommand();
                command.CommandText = "DeleteStoredProcedureName";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@ID", ID));
                try
                {
                    connection.Open();
                    int count = command.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {

                }
            }
        }

        public List<Bookmark> FindAll()
        {
            string connectionString = "EduGetItFromConfigurationFile";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                List<Bookmark> bookmarks = new List<Bookmark>();
                SqlCommand command = connection.CreateCommand();
                SqlDataReader reader = null;
                command.CommandText = "FindAllStoredProcedureName";
                command.CommandType = CommandType.StoredProcedure;
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        Bookmark bookmark = new Bookmark();
                        if (reader["Name"] != DBNull.Value)
                        {
                            bookmark.Name = Convert.ToString(reader["Name"]);
                        }
                        if (reader["Description"] != DBNull.Value)
                        {
                            bookmark.Name = Convert.ToString(reader["Description"]);
                        }
                        if (reader["BaseUri"] != DBNull.Value)
                        {
                            bookmark.Name = Convert.ToString(reader["BaseUri"]);
                        }
                        if (reader["Username"] != DBNull.Value)
                        {
                            bookmark.Name = Convert.ToString(reader["Username"]);
                        }
                        if (reader["Password"] != DBNull.Value)
                        {
                            bookmark.Name = Convert.ToString(reader["Password"]);
                        }
                        bookmarks.Add(bookmark);
                    }
                }
                catch (SqlException ex)
                {
                    //TODO: 
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                }
                return bookmarks;
            }
        }
 
    }
}